#include "mainwindow.h"
#include "ui_mainwindow.h"
 #include <QFileDialog>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/core/core.hpp>
#include "opencv2/imgcodecs.hpp"
#include <opencv2/highgui/highgui.hpp>
#  include <opencv2/imgcodecs/imgcodecs_c.h>
#include <stdio.h>
#include <iostream>
#include <QDebug>
#include <cvdialog.h>
#include <opencv2/core/utility.hpp>
extern "C"{
#include <libusb-1.0/libusb.h>
#include <stdio.h>
}
//using namespace std;
libusb_device *device;
libusb_device_handle *dev_handle;

unsigned char glInEpNum=0;
unsigned char glOutEpNum=0;
unsigned int  glInMaxPacketSize=0;
unsigned int  glOutMaxPacketSize=0;

//Print info about the buses and devices.
int print_info(libusb_device **devs)
{
    int i,j;
    int busNo, devNo;
    int err = 0;

    i = j = 0;

    printf("\nList of Buses and Devices attached :- \n\n");
    while ((device = devs[i++]) != NULL)
    {
        struct libusb_device_descriptor desc;

        int r = libusb_get_device_descriptor(device, &desc);
        if (r < 0)
        {
            printf("\n\tFailed to get device descriptor for the device, returning");
            return err;
        }

        printf("Bus: %03d Device: %03d: Device Id: %04x:%04x\n",
               libusb_get_bus_number(device),libusb_get_device_address(device),
               desc.idVendor, desc.idProduct );
    }
    printf("\nChoose the device to work on, From bus no. and device no. :-");
    fflush(stdout);
    printf("\nEnter the bus no : [e.g. 2]      :");
    err = scanf("%d",&busNo);
    printf("Enter the device no : [e.g. 5]  :");
//    err = scanf("%d",&devNo);

//    while ((device = devs[j++]) != NULL)
//    {
//        struct libusb_device_descriptor desc;

//        libusb_get_device_descriptor(device, &desc);

//        if ( busNo == libusb_get_bus_number(device))
//        {
//            if ( devNo == libusb_get_device_address(device))
//            {
//                printf("\n --------------------------------------------------------------------------------------------");
//                printf("\nYou have selected USB device : %04x:%04x:%04x\n\n", \
//                desc.idVendor, desc.idProduct,desc.bcdUSB);
//                return 0;
//            }
//        }
//    }

//    printf("\nIllegal/nonexistant device, please restart and enter correct busNo, devNo");
    return err;
}
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    qDebug() << "key : " ;
    //fprintf("dddd");
    int err;
    int usr_choice;
    ssize_t cnt;  // USB device count
    libusb_device **devs; //USB devices
    //scan the arguments amd make sence out of them
    //scan_arg();

    //initialise the libusb library
//    err = libusb_init(NULL);
//    if(err)
//    {
//        printf("\n\t The libusb library failed to initialise, returning");
//        //return(err);
//    }

//    //Detect all devices on the USB bus.
//    cnt = libusb_get_device_list(NULL, &devs);
//    //Detect all devices on the USB bus.
//        cnt = libusb_get_device_list(NULL, &devs);
//        if (cnt < 0)
//        {
//            printf("\n\t No device is connected to USB bus, returning ");
//            //return (int)cnt;
//        }

//        //print all devices on the bus
//        err = print_info(devs);
}

MainWindow::~MainWindow()
{
    delete ui;
}



void MainWindow::on_ok_btn_clicked()
{

}

void MainWindow::on_open_ccd_btn_clicked()
{
   CVDialog  *dia = new  CVDialog(this);
   dia->show();
//     cv::VideoCapture cap;
//        cap.open(0);
//        cv::Mat frame;
//        cap>>frame;
//        QImage img = QImage( (const unsigned char*)(frame.data), frame.cols, frame.rows, QImage::Format_RGB888 );
//    //    namedWindow( "Image_Show", WINDOW_AUTOSIZE );
//    //    imshow( "Image_Show", src );
//        ui->label->setPixmap( QPixmap::fromImage(img) );
}
